<?php
defined('_JEXEC') or die('Restricted access');

class igphotoController extends igbaseController
{
	function __construct($config = array())
	{
		parent::__construct($config);
		$this->registerTask('apply', 'save');
		$this->registerTask('save_and_next', 'save');
		
		$cid = JRequest::getVar('cid', array(0), '', 'array');
		$catid = JRequest::getInt('catid', 0);
		$catidString = !empty($catid) ? '&catid='.$catid : '';
		
		$this->redirect = 'index.php?option=com_igallery&controller=igphoto'.$catidString;
		$this->applyRedirect = 'index.php?option=com_igallery&controller=igphoto&view=edit_photo&cid[]='.$cid[0];
	}
	
	function copy_move()
	{
		$model = $this->getModel();
	
		if(!$model->copy_move()) 
		{
			JError::raise(2, 500, $model->getError() );
		}
	
		$copyMove = JRequest::getWord('copy_move','copy');
		$msg = $copyMove == 'move' ? JText::_('PHOTOS MOVED SUCCESSFULLY') : JText::_('PHOTOS COPIED SUCCESSFULLY');
		
		$this->setRedirect('index.php?option=com_igallery&controller=igphoto&catid='.JRequest::getInt('catid'), $msg);
	}
	
	function swfUpload()
	{
		$fileName = $_FILES['Filedata']['name'];
		$tmpPath = $_FILES['Filedata']['tmp_name'];
		$uploadError = $_FILES['Filedata']['error'];
		
		if(! $fileData = igFileHelper::processUploadedImage($fileName, $tmpPath, $uploadError, 'igallery_img', false) )
		{
			return false;
		}
		
		$model = $this->getModel();
		
		if (!$model->store($fileData) ) 
		{
			return false;
		}
		
		echo 1;
	}
	
	function plUpload()
	{
		$fileName = $_FILES['file']['name'];
		$tmpPath = $_FILES['file']['tmp_name'];
		$uploadError = $_FILES['file']['error'];
		
		if(! $fileData = igFileHelper::processUploadedImage($fileName, $tmpPath, $uploadError, 'igallery_img', false) )
		{
			return false;
		}
		
		$model = $this->getModel();
		
		if (!$model->store($fileData) ) 
		{
			return false;
		}
		
		echo 1;
	}
	
	function browserUpload()
	{
		for($i=0; $i<count($_FILES['uploads']['name']); $i++)
		{
			$fileName = $_FILES['uploads']['name'][$i];
			$tmpPath = $_FILES['uploads']['tmp_name'][$i];
			$uploadError = $_FILES['uploads']['error'][$i];
			
			if(! $fileData = igFileHelper::processUploadedImage($fileName, $tmpPath, $uploadError, 'igallery_img', true) )
			{
				$this->setRedirect('index.php?option=com_igallery&controller=igphoto&catid='.JRequest::getInt('catid'));
				return false;
			}
			
			$model = $this->getModel();
			
			if (!$model->store($fileData) ) 
			{
				return false;
			}
		}
		
		$this->setRedirect('index.php?option=com_igallery&controller=igphoto&catid='.JRequest::getInt('catid'));
	}
	
	function rotate()
	{
		$model = $this->getModel();
	
		if( !$model->rotate() ) 
		{
			$msg = '';
			JError::raise(2, 500, $model->getError() );
		}
		
		$this->setRedirect('index.php?option=com_igallery&controller=igphoto&catid='.JRequest::getInt('catid'));
	}
	
	function serverImport()
	{
		$filePathRaw = JRequest::GetVar('path');
		$filePath = str_replace('*','\\',$filePathRaw);
		
		
		if(! $fileData = igFileHelper::processImportedImage($filePath) )
		{
			return false;
		}
		
		$model = $this->getModel();
		
		if (!$model->store($fileData) ) 
		{
			echo $model->getError();
			return false;
		}
		
		echo 1;
	}
	
	function assignMenuImage()
	{
		$model = $this->getModel();
	
		if( !$model->assignMenuImage() ) 
		{
			JError::raise(2, 500, $model->getError() );
		}
		
		$this->setRedirect('index.php?option=com_igallery&controller=igphoto&catid='.JRequest::getInt('catid'));
	}
	
	function save()
	{
		$post	= JRequest::get('post');
		$model = $this->getModel();
		
		$nextOrder = $model->save($post);
		
		switch($this->_task)
		{
			case 'apply':
			$url = $this->applyRedirect;
			break;
			
			case 'save_and_next':
			$url = empty($nextOrder) ? $this->redirect : 'index.php?option=com_igallery&controller=igphoto&view=edit_photo&cid[]='.$nextOrder;
			break;
			
			case 'save':
			$url = $this->redirect;
		}
		
		$this->setRedirect($url, JText::_( 'SUCCESSFULLY SAVED' ));
	}
}	